﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRepository;
using Entities;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web.SessionState;

namespace MvcUI.Controllers
{
    public class AccountController : Controller
    {
         private IUserRepository repository;
         private static string verificationCode;
        public AccountController(IRepository.IUserRepository userRepository)
        {
            repository = userRepository;
        }

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public string LogIn()
        {
            
           
            string userName = Request["userName"].ToString();
            string password = Request["password"].ToString();
            string code = Request["Code"].ToString();
            if (verificationCode != code)
            {
                return "-1";//验证码错误
            }
            if (repository.Vefify(userName, password))
            {
                Entities.User user = repository.Get(userName, password);
                Session["UserID"] = user.ID;
                Session["UserName"] = user.UserName;
                Session["Verified"] = true;
                Session["Role"] = "Admin";
                return "1";//验证成功
            }
            else
            {
                return "0";//用户名或密码错误
            }
        }

        [HttpGet]
        public ActionResult LogOut()
        {
            Session["Verified"] = false;
            return RedirectToAction("Index", "Account");
           
        }

        public void Image()
        {
            System.Drawing.Bitmap Img = new Bitmap(70, 30);
            Graphics g = Graphics.FromImage(Img);

            //设置画布背景色
            g.Clear(Color.White);

            //背景噪点
            Random rd = new Random();
            for (int i = 0; i < 100; i++)
            {
                int startx = rd.Next(1, 199);
                int starty = rd.Next(1, 39);
                g.DrawLine(new Pen(Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255)), 4), startx, starty, startx + rd.Next(-3, 3), starty + rd.Next(-3, 3));
            }
            for (int j = 0; j < 20; j++)
            {
                int startx = rd.Next(1, 199);
                int starty = rd.Next(1, 39);
                g.DrawLine(new Pen(Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255))), startx, starty, startx + rd.Next(-100, 100), starty + rd.Next(-100, 100));
            }
            //边框
            g.DrawRectangle(new Pen(Color.Black), 0, 0, 70 - 1, 30 - 1);

            string s = rd.Next(0, 9).ToString() + rd.Next(0, 9).ToString() + rd.Next(0, 9).ToString() + rd.Next(0, 9).ToString() ;
            verificationCode = s;
            //将验证码画在画布上
            for (int i = 0; i < s.Length; i++)
            {
                g.DrawString(s[i].ToString(), new System.Drawing.Font("Arial", 16), new System.Drawing.SolidBrush(Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255))), i * 15, 4);
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            Response.ClearContent();//需要输出图象信息 要修改HTTP头 
            Response.ContentType = "image/Png";
            Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            Img.Dispose();
            Response.End();
        }

    }
}
